﻿using Microsoft.EntityFrameworkCore;
using System.Linq.Expressions;

namespace CustomRepository
{
    public class BaseRepository<T>:IBaseRepository<T> where T : class,new()
    {

        protected readonly EFCoreContext _context;
        public BaseRepository(EFCoreContext context)
        {
            _context = context;
        }

        public async Task<int> AddAsync(T t)
        {
            await _context.AddAsync(t);
            return await _context.SaveChangesAsync();
        }

        public async Task<List<T>> GetAllAsync()
        {
            return await _context.Set<T>().ToListAsync();
        }

        public async Task<List<T>> GetAsync(Expression<Func<T, bool>> expre)
        {
            return await _context.Set<T>().Where(expre).ToListAsync();
        }

        public async Task<T> GetModelAsync(int id)
        {
            return await _context.Set<T>().FindAsync(id);
        }

        public async Task<int> Update(T t)
        {
            _context.Update(t);
            return await _context.SaveChangesAsync();
        }
    }
}
